import siteInfo from 'siteinfo.js';
App({
    onLaunch() {

        this.setApiRoot();

    },
    globalData: {
        userInfo: null
    },
    api_root: '', // api地址
    setApiRoot() {
        let App = this;
        App.api_root = `${siteInfo.siteroot}`;

    },

    /**
     * post提交
     */
    _post_form(url, data, success, fail, complete) {
        wx.showNavigationBarLoading();
        let App = this;
        // 构造请求参数
        data = Object.assign({
            token: wx.getStorageSync('token')
        }, data);
        wx.request({
            url: App.api_root + url,
            header: {
                'content-type': 'application/x-www-form-urlencoded',
            },
            method: 'POST',
            data,
            success(res) {
                console.log("ffff", res.data.status);
                if (res.statusCode !== 200 || typeof res.data !== 'object') {
                    App.showError('网络请求出错');
                    return false;
                }
                if (res.data.status === 401) {
                    // 登录态失效, 重新登录
                    wx.navigateTo({
                        url: "/pages/index/index",
                    })
                    
                } else if (res.data.status != 200) {

                    App.showError(res.data.message, () => {
                        fail && fail(res);
                    });
                    return false;
                }
                success && success(res.data);
            },
            fail(res) {
                // console.log(res);
                App.showError(res.errMsg, () => {
                    fail && fail(res);
                });
            },
            complete(res) {
                wx.hideLoading();
                wx.hideNavigationBarLoading();
                complete && complete(res);
            }
        });
    },

    /**
     * 显示成功提示框
     */
    showSuccess(msg, callback) {
        wx.showToast({
            title: msg,
            icon: 'success',
            success() {
                callback && (setTimeout(() => {
                    callback();
                }, 1500));
            }
        });
    },

    /**
     * 显示失败提示框
     */
    showError(msg, callback) {
        wx.showModal({
            title: '友情提示',
            content: msg,
            showCancel: false,
            success(res) {
                // callback && (setTimeout(() => {
                //   callback();
                // }, 1500));
                callback && callback();
            }
        });
    },


})